// g++ test_rabbitmq.cpp RabbitmqClient.cpp amqpclient.cpp -lrabbitmq -lpthread
#include <iostream>
#include "RabbitmqClient.h"
#include <thread>

std::string vecRecvMsg;

extern bool bhave;

int test_class1()
{
    CRabbitmqClient objRabbitmq;

    std::string strIP = "localhost";
    int iPort = 5672;
    std::string strUser = "test";
    std::string strPasswd = "test";

    int iRet = objRabbitmq.Connect(strIP, iPort, strUser, strPasswd);
    printf("Rabbitmq Connect Ret: %d\n", iRet);

    std::string strExchange = "ExchangeTest";
    std::string strRoutekey = "routekeyTest";

    //// 可选操作 Declare Exchange
    // iRet = objRabbitmq.ExchangeDeclare(strExchange, "direct");
    // printf("Rabbitmq ExchangeDeclare Ret: %d\n", iRet);

    //// 可选操作（接收） Declare Queue
    // iRet = objRabbitmq.QueueDeclare(strQueuename);
    // printf("Rabbitmq QueueDeclare Ret: %d\n", iRet);

    //// 可选操作（接收） Queue Bind
    // iRet = objRabbitmq.QueueBind(strQueuename, strExchange, strRoutekey);
    // printf("Rabbitmq QueueBind Ret: %d\n", iRet);

    // Send Msg
    std::string strSendMsg1 = "rabbitmq send test msg1";
    std::string strSendMsg2 = "rabbitmq send test msg2";
    iRet = objRabbitmq.Publish(strSendMsg1, strExchange, strRoutekey);
    printf("Rabbitmq Publish 1 Ret: %d\n", iRet);

    iRet = objRabbitmq.Publish(strSendMsg2, strExchange, strRoutekey);
    printf("Rabbitmq Publish 2 Ret: %d\n", iRet);

    // Recv Msg
    std::thread([](CRabbitmqClient *client)
                {
    std::string strQueuename = "4.3keti";

    int iRet = client->Consumer(strQueuename, vecRecvMsg, 1);
    printf("Rabbitmq Consumer Ret: %d\n", iRet);

    client->Disconnect(); },
                &objRabbitmq)
        .detach();

    std::thread([]()
                {
    while (1)
    {
        if (bhave)
        {
            printf("Consumer: %s\n", vecRecvMsg.c_str());

            bhave = false;
        }
    } })
        .detach();

    while (1)
    {
    }

    return 0;
}

#include "amqpclient.h"

void test_class2()
{
    const char *hostname = "localhost";
    const int port = 5672;
    const char *user = "test";
    const char *pwd = "test";
    const char *vhost = "/";
    const char *queuename = "queuename001";

    AmqpClient amqpClient(hostname, port, user, pwd, vhost, queuename);
    amqpClient.ConnectRabbitmq();
    printf("connect rabbitmq succ, is consuming!\n");
    amqpClient.StartConsumer();
}

int main(int argc, char *argv[])
{
    //test_class1();
     test_class2();
}
